package com.talkweb.newproject.admin.rest;

import com.talkweb.newproject.admin.sysmenu.dto.SysMenuDTO;
import com.talkweb.newproject.admin.sysmenu.service.SysMenuService;
import com.talkweb.newproject.api.model.admin.SysMenuParam;
import com.talkweb.newproject.api.model.common.ResponseResult;
import com.talkweb.newproject.api.model.common.Status;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by qsf on 2019/5/7.
 */
@RestController
@RequestMapping(value="/api/sysMenu")
@Slf4j
public class SysMenuRest {
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 新增菜单数据
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/create/v1",method= RequestMethod.POST)
    public ResponseResult create(@RequestBody SysMenuDTO obj) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            sysMenuService.insertSysMenu(obj);
            result.setStatus(Status.SUCCESS);
            result.setMessage("保存成功");
            return result;
        }catch(Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    /**
     * 修改菜单信息
     * @param sysMenu
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/update/v1",method=RequestMethod.POST)
    public ResponseResult update(@RequestBody SysMenuDTO sysMenu) throws Exception {
        ResponseResult result = new ResponseResult();
        try{

            sysMenuService.updateSysMenu(sysMenu);
            result.setStatus(Status.SUCCESS);
            result.setMessage("修改成功");
            return result;
        }catch(Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试!");
            return result;
        }
    }

    /***
     * 删除菜单信息
     * @param ids
     * @return
     * @throws Exception
     */
    @RequestMapping(value="delete/v1",method = RequestMethod.POST)
    public ResponseResult deleteSysMenu(@RequestParam String ids) throws Exception {
        ResponseResult result = new ResponseResult();
        try {
            if(ids!= null && !"".equals(ids)) {
                sysMenuService.deleteSysMenu(ids);
                result.setStatus(Status.SUCCESS);
                result.setMessage("删除成功!");

            }else {
                result.setStatus(Status.FAILD);
                result.setMessage("删除失败");

            }
            return result;
        }catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试 ");
            return result;
        }

    }

    /**
     * 根据id查询菜单信息
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/get/v1/{id}",method = RequestMethod.POST)
    @ApiOperation(value="根据id查询菜单信息",notes="返回结果,SUCCESS:200,FAILD:500",httpMethod="POST")
    public ResponseResult<SysMenuDTO> querySysMenuByPrimaryKey(@PathVariable("id") String id) {
        ResponseResult<SysMenuDTO> result = new ResponseResult<>();
        try {
            SysMenuDTO sysMenuDTO = sysMenuService.querySysMenuByPrimaryKey(id);
            result.setStatus(Status.SUCCESS);
            result.setData(sysMenuDTO);
            result.setMessage("查询成功");
            return result;
        } catch (Exception e) {
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    /**
     * 根据条件查询单个菜单信息
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/queryLike",method = RequestMethod.POST)
    public ResponseResult<SysMenuDTO> queryLike(@RequestBody SysMenuDTO obj) throws Exception{
        ResponseResult<SysMenuDTO> result = new ResponseResult<>();
        try {
            SysMenuDTO sysMenuDTO = sysMenuService.queryLikeSysMenu(obj);
            result.setStatus(Status.SUCCESS);
            result.setData(sysMenuDTO);
            result.setMessage("查询成功");
            return result;
        }catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }


    }

    @ResponseBody
    @RequestMapping(value="/getSysMenuList",method=RequestMethod.POST)
    public ResponseResult<List<SysMenuDTO>> querySysMenuList(@RequestBody SysMenuParam obj) throws Exception {
        ResponseResult<List<SysMenuDTO>> result = new ResponseResult<>();
        try {
            List<SysMenuDTO> sysMenuDTOList  = sysMenuService.searchSysMenu(obj);
            result.setStatus(Status.SUCCESS);
            result.setData(sysMenuDTOList);
            result.setMessage("查询成功");
            return result;
        }catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试!");
            return result;
        }

    }
}
